package com.statistics.mapper;

import com.alibaba.fastjson2.JSONObject;
import com.statistics.domain.vo.*;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 教育部年度统计Mapper
 *
 * @author chenyy
 * &#064;date 2025/3/10 11:42
 */
public interface EducationMapper {

    List<ExportUnitVo> exportUnit(@Param("type") Integer type);

    List<ExportPersonVo> exportPerson();

    List<ExportResAchieveVo> exportResAchieve(@Param("year") String year, @Param("type") Integer type);

    List<ExportProjectVo> exportProject(@Param("year") String year, @Param("type") Integer type);

    List<ExportHonorVo> exportHonor(@Param("year") String year, @Param("type") Integer type);

    List<ExportResUnitVo> exportResUnit(@Param("year") String year, @Param("type") Integer type);

    @MapKey("name")
    List<Map> exportResPerson();

    @MapKey("name")
    List<Map> exportActivityUnit(@Param("year") String year, @Param("type") Integer type);

    @MapKey("name")
    List<Map> exportScienceProject(@Param("year") String year, @Param("type") Integer type);

    @MapKey("name")
    List<Map> exportScienceAchievement(@Param("year") String year, @Param("type") Integer type);

    @MapKey("name")
    List<Map> exportPublishBook(@Param("year") String year, @Param("type") Integer type);

    @MapKey("name")
    List<Map> exportScienceHonor(@Param("year") String year, @Param("type") Integer type);

    // 科技交流派遣/接收
    @Select("select count(*) total, count(case when research_type_id = 1 then 1 end) as internal, " +
            "count(case when research_type_id = 2 then 1 end) as foreigns from ${table} where year(begin_date) = #{year} ")
    JSONObject getPersonnel(@Param("year") String year, @Param("table") String table);

    // 科技交流 出席人员/特邀报告
    @Select("select ifnull(sum(${countField}),0) total, ifnull(sum(case when meeting_scope = 1 then ${countField} end),0) as internal, " +
            "ifnull(sum(case when meeting_scope = 2 then ${countField} end),0) as foreigns from ${table} where meeting_type_id = 1  and year(${yearDate}) = #{year}")
    JSONObject getPersonnelReport(@Param("year") String year, @Param("table") String table, @Param("countField") String countField, @Param("yearDate") String yearDate);

    // 科技交流 交流论文
    @Select("select ifnull(sum(total),0) as total, sum(internal) as internal, sum(foreigns) as foreigns from " +
            "( select sum(paper_number) as total, ifnull(sum(case when meeting_scope = 1 then paper_number end), 0) as internal, " +
            "ifnull(sum(case when meeting_scope = 2 then paper_number end), 0) as foreigns from biz_meeting " +
            "where meeting_type_id = 1 and year(begin_date) = #{year} " +
            "union all select sum(paper_num) as total, ifnull(sum(case when meeting_scope = 1 then paper_num end), 0) as internal, " +
            "ifnull(sum(case when meeting_scope = 2 then paper_num end), 0) as foreigns from biz_join_meeting " +
            "where meeting_type_id = 1 and year(meeting_date) = #{year}) as combined_data")
    JSONObject getMeetingPaper(@Param("year") String year);

    // 科技交流 主板会议
    @Select("select count(*) total, count(case when meeting_scope = 1 then 1 end) as internal, " +
            "count(case when meeting_scope = 2 then 1 end) as foreigns from biz_meeting " +
            "where meeting_type_id = 1 and year(begin_date) = #{year}")
    JSONObject getMeeting(@Param("year") String year);
}